<?php

if( !defined("databaseData_class") );
{
	define("databaseData_class", "true", false);

   class databaseData   
   {
   		var $DB_PREFIX;
      	var $TABLE_ADMIN = 'admin';
      	var $TABLE_CATEGORY = 'category';
      	var $TABLE_DATA = 'data';
      	var $TABLE_DEPARTMENT = 'department';
      	var $TABLE_DEPT_PERMS = 'dept_perms';
      	var $TABLE_DEPT_REVIEWER = 'dept_reviewer';
      	var $TABLE_LOG = 'log';
      	var $TABLE_RIGHTS = 'rights';
      	var $TABLE_USER = 'user';
      	var $TABLE_USER_PERMS = 'user_perms';
      	var $FORBIDDEN_RIGHT = -1;
		var $NONE_RIGHT = 0;
		var $VIEW_RIGHT = 1;
		var $READ_RIGHT = 2;
		var $WRITE_RIGHT = 3;
		var $ADMIN_RIGHT = 4;
   		var $name;
		var $id;
		var $connection;
		var $database;
		var $tablename;
		var $error;
		var $field_name;
		var $field_id;
		var $result_limit;  

	function databaseData($id, $connection, $database)
	{
		$this->connection = $connection;
		$this->database = $database;
		$this->setId($id); 
		$this->result_limit = 1;
	}
	function setTableName($tablename)
	{	
		$this->tablename = "$tablename";	
	}
	function setId($id)
	{
		
		$this->id = $id;
		$this->name = $this->findName();
	}
	function setName($name)
	{
		
		$this->name = $name;
		$this->id = findId();
	}
	function getName()
	{
		return $this->name;	
	}

	function getId()
	{
		return $this->id;	
	}
	
	function findId()
	{
		$query = "SELECT {$this->database}.{$GLOBALS['CONFIG']['db_prefix']}{$this->tablename}.$this->field_id FROM {$this->database}.{$GLOBALS['CONFIG']['db_prefix']}{$this->tablename} WHERE {$this->database}.{$GLOBALS['CONFIG']['db_prefix']}{$this->tablename}.$this->field_name='$this->name'";
		$result = mysql_query($query, $this->connection) or die ("Error in query: $query. " . mysql_error());

		if( mysql_num_rows($result) > $this->result_limit AND result_limit != 'UNLIMITED')
		{
			$this->error='Error: non-unique';
		}
		elseif (mysql_num_rows($result) == 0) 
		{
			$this->error = 'Error: unable to fine id in database';
		}
		else
		{
			list($id) = mysql_fetch_row($result);
		}
		return $id;
	}
	
	
	function findName()
	{
		$name = '';
		$query = "SELECT {$this->database}.{$GLOBALS['CONFIG']['db_prefix']}$this->tablename.$this->field_name FROM {$this->database}.{$GLOBALS['CONFIG']['db_prefix']}$this->tablename WHERE {$this->database}.{$GLOBALS['CONFIG']['db_prefix']}{$this->tablename}.$this->field_id = $this->id";
		$result = mysql_query($query, $this->connection) or die ("Error in query: " .$query . mysql_error());
		
		if(mysql_num_rows($result) > $this->result_limit AND result_limit != 'UNLIMITED')
		{
			$this->error='Error: non-unique';
		}
		elseif (mysql_num_rows($result) == 0)
		{	
			$this->error = 'Error: unable to find id in database';
		}
		else
		{
			list($name) = mysql_fetch_row($result);
                        return $name;
		}
	}
	function reloadData() 
	{
				
		$this->setId($this->id);
	}
	
	function getError()
	{	
		return $this->error;	
	}

	function combineArrays($high_priority_array, $low_priority_array)
	{
		$found = false;
		$result_array = array();
		$result_array = $high_priority_array;
		$result_array_index = sizeof($high_priority_array);
		for($l = 0 ; $l<sizeof($low_priority_array); $l++) 
		{
			
			for($r = 0; $r<sizeof($result_array); $r++)
			{
				if($result_array[$r] == $low_priority_array[$l])
				{
					
					$r = sizeof($result_array);
					$found = true;
				}
			}
			
			if(!$found)
			{
				$result_array[$result_array_index++] = $low_priority_array[$l];
			}
			$found = false;
		}
		return $result_array;
	}
	function convertToFileDataOBJ($fid_array)
	{
		$filedata_array = array();
		for($i = 0; $i<sizeof($fid_array); $i++)
		{
			$filedata_array[$i] = new FileData($fid_array[$i], $this->connection, $this->database, "data");
		}
		return $filedata_array;
	}

   }

}
?>
